package net.guoyixian.common.redis;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class RedisService {

	@Autowired(required = false)
	private ShardedJedisPool shardedJedisPool;

	private <T> T execute(Function<ShardedJedis, T> function) {
		ShardedJedis shardedJedis = null;
		try {
			// 从连接池中获取到jedis分片对象
			shardedJedis = shardedJedisPool.getResource();
			return function.execute(shardedJedis);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != shardedJedis) {
				// 关闭，检测连接是否有效，有效则放回到连接池中，无效则重置状态
				shardedJedis.close();
			}
		}
		return null;
	}

	/**
	 * 保存数据到redis中
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public String set(final String key, final String value) {
		return this.execute(new Function<ShardedJedis, String>() {
			@Override
			public String execute(ShardedJedis shardedJedis) {
				return shardedJedis.set(key, value);
			}

		});
	}

	/**
	 * 保存数据到redis中，生存时间单位是：秒
	 * 
	 * @param key
	 * @param value
	 * @param seconds
	 * @return
	 */
	public String set(final String key, final String value,
			final Integer seconds) {
		return this.execute(new Function<ShardedJedis, String>() {
			@Override
			public String execute(ShardedJedis shardedJedis) {
				String result = shardedJedis.set(key, value);
				shardedJedis.expire(key, seconds);// 设置生存时间
				return result;
			}

		});
	}

	/**
	 * 保存数据到redis中，生存时间单位是：秒
	 * 
	 * @param key
	 * @param value
	 * @param seconds
	 * @return
	 */
	public String set(final byte[] key, final byte[] value,
			final Integer seconds) {
		return this.execute(new Function<ShardedJedis, String>() {
			@Override
			public String execute(ShardedJedis shardedJedis) {
				String result = shardedJedis.set(key, value);
				shardedJedis.expire(key, seconds);// 时间为秒
				return result;
			}

		});
	}

	/**
	 * 从redis中获取数据
	 * 
	 * @param key
	 * @return
	 */
	public String get(final String key) {
		return this.execute(new Function<ShardedJedis, String>() {
			@Override
			public String execute(ShardedJedis shardedJedis) {
				return shardedJedis.get(key);
			}

		});
	}

	/**
	 * 从redis中获取数据
	 * 
	 * @param key
	 * @return
	 */
	public byte[] get(final byte[] key) {
		return this.execute(new Function<ShardedJedis, byte[]>() {
			@Override
			public byte[] execute(ShardedJedis shardedJedis) {
				return shardedJedis.get(key);
			}

		});
	}

	/**
	 * 设置key生存时间，单位：秒
	 * 
	 * @param key
	 * @param seconds
	 * @return
	 */
	public Long expire(final String key, final Integer seconds) {
		return this.execute(new Function<ShardedJedis, Long>() {
			@Override
			public Long execute(ShardedJedis shardedJedis) {
				return shardedJedis.expire(key, seconds);
			}
		});
	}

	/**
	 * 从redis中删除数据
	 * 
	 * @param key
	 * @return
	 */
	public Long del(final String key) {
		return this.execute(new Function<ShardedJedis, Long>() {
			@Override
			public Long execute(ShardedJedis shardedJedis) {
				return shardedJedis.del(key);
			}
		});
	}

	/**
	 * 从redis中删除数据
	 * 
	 * @param key
	 * @return
	 */
	public Long del(final byte[] key) {
		return this.execute(new Function<ShardedJedis, Long>() {
			@Override
			public Long execute(ShardedJedis shardedJedis) {
				return shardedJedis.del(key);
			}
		});
	}

	/**
	 * 获取所有集合
	 * 
	 * @param key
	 * @return
	 */
	public Set<byte[]> keys(final byte[] key) {
		return this.execute(new Function<ShardedJedis, Set<byte[]>>() {
			@Override
			public Set<byte[]> execute(ShardedJedis shardedJedis) {
				// TODO Auto-generated method stub
				Jedis j = shardedJedis.getShard(key);
				return j.keys(key);
			}
		});
	}
}